<button id="myBtn" title="Go to top"></button>
<div class="header-top " id="sticky">
    <div class="container">
        <div class="row">
            <div class="col-xl-2 col-lg-2 col-md-2 col-sm-6 col-6">
                <div class="text-left header-top-left">
                    <?php
                    session_start();
                    require_once 'conn.php';
                    // 获取设置信息
                    $sql1 = "SELECT image FROM setting WHERE id = 1";
                    $result1 = $conn->query($sql1);
                    $setting = $result1->fetch_assoc();
                    ?>

                    <a href="index.php">
                        <img src="<?php echo htmlspecialchars($setting['image'] ?? 'assets/image/logo/Stylixe.png'); ?>" class="img-responsive img logo" alt="Stylixe">
                    </a>
                    <div class="navbar-header d-xl-none d-lg-none">
                        <button type="button" class="btn-navbar navbar-toggle" onclick="openNav()" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                            <i class="fas fa-bars"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="col-xl-6 col-lg-12 col-md-6 col-sm-6 col-6 res_ap d-flex justify-content-around">
                <div class="d-flex justify-content-between">
                    <div id="mySidenav" class="sidenav">
                        <div class="close-nav d-xl-none d-lg-none">
                            <span class="categoriesss">导航栏</span>
                            <a href="javascript:void(0)" class="closebtn pull-right" onclick="closeNav()"><i class="fas fa-times"></i></a>
                        </div>
                        <ul class="main-menu navbar">
                            <li><a href="index.php" class="hello">首页</a></li>
                            <li><a href="shop-sidebar.php" class="hello">商品列表</a></li>
                            <li><a href="news.php" class="hello">新闻资讯</a></li>
                            <li><a href="profile.php" class="hello">个人中心</a></li>
                            <li><a href="my_orders.php" class="hello">我的订单</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div style="margin-left: 300px;">
                <form class="d-flex justify-content-end ms-auto side-icon">
                    <div class="search-container ">
                        <!-- 用户登录 -->
                        <?php
                        // 获取用户ID
                        $user_id = $_SESSION['user_id'];

                        // 查询用户的头像路径
                        $sql = "SELECT avatar FROM users WHERE id = ?";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("i", $user_id);
                        $stmt->execute();
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            $user = $result->fetch_assoc();
                            $avatar_path = $user['avatar']; // 获取头像路径
                        } else {
                            $avatar_path = 'assets/image/avater/default.jpg'; // 如果没有头像，则使用默认头像
                        }
                        ?>
                        <!-- 用户登录 -->
                        <div class="desktop-user desktop d-inline-block">
                            <?php if (empty($_SESSION['username'])): ?>
                                <!-- 未登录状态 -->
                                <div class="dropdown right1 md_acc button-group2 shop_drop3">
                                    <a href="#" class="symbol2 pro_user" data-toggle="dropdown" data-target="dropdown"></a>
                                    <div class="dropdown-menu r_menu dropdown-menu-right shop_nav_drop3">
                                        <a class="dropdown-item font-weight-bolder" href="login.php">
                                            <span class="m-space"><i class="fas fa-user"></i></span>
                                            <span class="login">&nbsp;用户登录</span>
                                        </a>
                                        <a class="dropdown-item font-weight-bolder" href="registration.php">
                                            <span class="m-space"><i class="fas fa-user-plus"></i></span>
                                            <span class="register">用户注册</span>
                                        </a>
                                        <a class="dropdown-item font-weight-bolder" href="admin_login.php">
                                            <span class="m-space"><i class="fas fa-cog"></i></span>
                                            <span class="register">&nbsp;后台登录</span>
                                        </a>
                                    </div>
                                </div>
                            <?php else: ?>
                                <!-- 已登录状态 -->
                                <div class="dropdown right1 md_acc button-group2 shop_drop3">
                                    <a href="#" data-toggle="dropdown" data-target="dropdown">
                                        <img src="<?php echo htmlspecialchars($avatar_path); ?>" class="useravatar" alt="用户头像">
                                    </a>
                                    <div class="dropdown-menu r_menu dropdown-menu-right shop_nav_drop3">
                                        <span class="dropdown-item font-weight-bolder">欢迎:<?php echo htmlspecialchars($_SESSION['username']); ?></span>
                                        <a class="dropdown-item font-weight-bolder" href="profile.php">
                                            <span class="m-space"><i class="fas fa-user"></i></span>
                                            <span class="profile">个人中心</span>
                                        </a>
                                        <a class="dropdown-item font-weight-bolder" href="logout.php">
                                            <span class="m-space"><i class="fas fa-sign-out-alt"></i></span>
                                            <span class="logout">退出登录</span>
                                        </a>
                                    </div>
                                </div>
                        </div>
                        <!-- desktop-Add-to-cart -->
                        <?php
                                // 查询购物车中的商品信息
                                $sql = "SELECT c.id, c.product_id, c.quantity, c.total, p.name, p.price, p.image 
        FROM cart c
        JOIN products p ON c.product_id = p.id
        WHERE c.user_id = $user_id";
                                $result = mysqli_query($conn, $sql);

                                // 计算总金额和商品数量
                                $total_amount = 0;
                                $total_items = 0;
                                $cart_empty = true; // 标记购物车是否为空

                                while ($row = mysqli_fetch_assoc($result)) {
                                    $total_amount += $row['total'];
                                    $total_items += $row['quantity'];
                                    $cart_empty = false; // 购物车有商品，标记为非空
                                }
                        ?>

                        <div class="desktop-cart desktop d-inline-block shop_drop4">
                            <div class="navbar">
                                <div class="dropdown d-inline-block button-group2">
                                    <a href="#" class="symbol2 pro_basket" data-toggle="dropdown" data-target="dropdown">
                                        <span class="fa-layers-counter">
                                            <?php echo "$total_items"; ?> <!-- 显示购物车中商品的数量 -->
                                        </span>
                                    </a>
                                    <div class="d-inline-block doller-position">
                                        <h6 class="doller">
                                            ￥<?php echo number_format($total_amount, 2); ?> <!-- 显示购物车的总金额 -->
                                        </h6>
                                    </div>
                                    <div class="dropdown-menu s_cart dropdown-menu-right shop_nav_drop4">
                                        <ul class="shopping-cart">
                                            <li class="d-block text-muted font-weight-bolder p-2 border-bottom">
                                                <span class="text-left cart-subtotal">总金额:</span>
                                                <span class="float-right cart-subtotal">￥<?php echo number_format($total_amount, 2); ?></span>
                                            </li>
                                            <li class="d-block font-weight-bolder pt-2">
                                                <span class="text-left no-hover"><a href="cart.php">进入购物车</a></span>
                                                <?php if (!$cart_empty): ?>
                                                    <span class="float-right"><a href="checkout.php">立即结算</a></span> <!-- 购物车不为空，显示结算按钮 -->
                                                <?php else: ?>
                                                    <span class="float-right text-muted">暂无商品</span> <!-- 购物车为空，禁用结算按钮 -->
                                                <?php endif; ?>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- desktop wallet -->
                    <?php endif; ?>
                    </div>
            </div>
            </form>
        </div>
    </div>
    <div class="row ">
        <div class="container appandd_new"></div>
    </div>
</div>
</div>